package com.situ.stl.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.stl.Service.CourseService;
import com.situ.stl.model.Course;
import com.situ.stl.util.JsonResult;
import jakarta.validation.constraints.Size;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/course")
public class CourseApi {
    private CourseService courseService;


    @Autowired
    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }

    //配置文件获取服务器端口
    @Value("${server.port:8080}")
    private String serverPort;

    private void buildFullImageUrl(Course course) {
        String serverBaseUrl = "http://localhost:8080";
        if (course.getPhoto() != null && !course.getPhoto().startsWith("http")) {
            course.setPhoto(serverBaseUrl + course.getPhoto());
        }
    }


    @GetMapping
    public JsonResult<Page<Course>> findAll(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Course course) {
        Page<Course> page = new Page<>(pageNo, pageSize);
        Page<Course> result = courseService.findAllWithCoachName(page, course);

        //处理分页结果中所有课程图片路径
        result.getRecords().forEach(this::buildFullImageUrl);

        return JsonResult.success(result);
    }



    //批量查询会员
    @PostMapping("/batch")
    public ResponseEntity<JsonResult<?>> getByIds(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return ResponseEntity.ok(JsonResult.fail("请提供课程ID列表"));
        } else {
            //8.20
            List<Course> courses = courseService.getByIds(ids);
            courses.forEach(this::buildFullImageUrl);

            return ResponseEntity.ok(JsonResult.success(courseService.getByIds(ids)));
        }
    }


    //删除
    @DeleteMapping
    public ResponseEntity<JsonResult<?>> deleteByIds(
            @RequestBody
            @Validated
            @Size(min = 1, message = "删除的课程数量至少为1")
            Integer[] ids
    ) {
        int count = this.courseService.deleteByIds(List.of(ids));

        if (count == 0) {
            return ResponseEntity.ok(JsonResult.fail("删除失败"));
        } else {
            return ResponseEntity.ok(JsonResult.success(count));
        }
    }

    //保存课程信息
    @PostMapping
    public ResponseEntity<JsonResult<?>> sava(@RequestBody @Validated Course course) {
        boolean success = this.courseService.save(course);

        if (success) {
//            保存成功处理图片
            buildFullImageUrl(course);

            return ResponseEntity.ok(JsonResult.success(course));
        } else {
            return ResponseEntity.ok(JsonResult.fail("保存课程失败"));
        }
    }

    //修改课程信息
    @PutMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody @Validated Course course) {
        boolean success = this.courseService.update(course);

        if (success) {
//            修改成功后处理图片
            buildFullImageUrl(course);

            return ResponseEntity.ok(JsonResult.success(course));
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改课程失败"));
        }
    }
}
